<?php
/**
 * 循环创建目录
 *
 * @param unknown $dir
 * @param number $mode
 * @return boolean
 */
function make_dirs($dir, $mode = 0777) {
	if (is_dir ( $dir ) || mkdir ( $dir, $mode ))
		return true;
	if (! make_dirs ( dirname ( $dir ), $mode ))
		return false;
	return mkdir ( $dir, $mode );
}

/**
 * 格式化tag
 *
 * @param unknown_type $tags
 */
function make_tags($tags) {
	$data = array ();
	$tagStr = '';
	$strA = array (
			' ',
			'、',
			'，'
	);
	$strB = ',';
	$tag = explode ( ',', str_replace ( $strA, $strB, $tags ) );
	$tag = array_unique ( $tag );
	foreach ( $tag as $k => $v ) {
		if (trim ( $v )) {
			$data ['tags'] [] = $v;
			$tagStr .= $v . ',';
		}
	}
	$data ['str'] = rtrim ( $tagStr, ',' );
	return $data;
}
/**
 * 截取字符串
 *
 * @param unknown $str
 * @param number $len
 * @param string $end
 * @return string
 */
function cut_str($str, $len = 200, $end = ' ...') {
	$str = strip_tags ( $str );

	$strlen = mb_strlen ( $str, 'utf-8' );
	if ($strlen >= $len) {
		$strEnd = $end;
	}
	$str = preg_replace ( "/^[(\xc2\xa0)|\s]+/", "", $str );
	$str = preg_replace ( "/\s+/", " ", $str );
	$str = mb_substr ( $str, 0, $len, 'utf-8' );
	return $str . $strEnd;
}
/**
 * 分页样式
 *
 * @param unknown $total
 *        	总条数
 * @param unknown $page
 *        	当前页
 * @param unknown $num
 *        	每页多少条
 */
function page_html($total, $page, $num) {
	$url = eregi_replace ( "(#.+$|page=[0-9]+)", '', $_SERVER ['REQUEST_URI'] );
	$url = $url . (strpos ( $url, '?' ) ? '' : "?");
	$url = eregi_replace ( "(&+)", '&', $url );
	$url = rtrim ( $url, '&' );

	$page = intval ( $page ) < 1 ? 1 : $page;
	$total_page = ceil ( $total / $num );
	if ($total_page > $page) {
		$next_page = '<li><a href="' . $url . '&page=' . ($page + 1) . '" class="next">下一页</a></li>';
	}
	if ($page != 1) {
		$first_page = '<li><a href="' . rtrim ( $url, '?' ) . '" class="next">首页</a></li>';
	}
	$str = '';
	$j = 0;
	$bar_num = 3;
	for($i = $page; ($page + $bar_num) > $i; $i ++) {
		if ($i <= $total_page) {
			if ($i == $page) {
				$str .= '<li><a href="' . $url . '&page=' . $i . '" class="hover">' . $i . '</a></li>';
			} else {
				$str .= '<li><a href="' . $url . '&page=' . $i . '">' . $i . '</a></li>';
			}
			$j ++;
		}
	}
	$bar_num = $bar_num - $j + 2;
	$n = 0;
	for($i = ($page - $bar_num); $n < $bar_num; $i ++, $n ++) {
		if ($i <= $total_page && $i > 1) {
			if ($i == $page) {
				$str1 .= '<li><a href="' . $url . '&page=' . $i . '" class="hover">' . $i . '</a></li>';
			} else {
				$str1 .= '<li><a href="' . $url . '&page=' . $i . '">' . $i . '</a></li>';
			}
		}
	}
	$html = $first_page . $str1 . $str . $next_page;
	$html = str_replace ( '?&', '?', $html );
	return $html;
}
/**
 * 设置cookie并加密
 *
 * @param unknown $name
 * @param unknown $value
 * @param unknown $opt
 */
function add_cookie($name, $value, $opt = array ( 'expire' => null  )) {
	if ($value) {
		$val = $value . substr ( rand ( 1000, 9999 ), 0, 4 ) . '_' . substr ( md5 ( $value ), 0, 10 ) . md5 ( date ( 'y' ) );
		cookie ( $name, $val, $opt );
	} else {
		cookie ( $name, NULL );
	}
}

/**
 * 获取cookie并解密
 *
 * @param unknown $name
 * @return string boolean
 */
function get_cookie($name) {
	$value = cookie ( $name );
	$arr = explode ( '_', $value );
	$arr [0] = substr ( $arr [0], 0, - 4 );
	$code = substr ( md5 ( $arr [0] ), 0, 10 ) . md5 ( date ( 'y' ) );
	if ($arr [1] == $code) {
		return $arr [0];
	} else {
		return false;
	}
}

/**
 * 把标签加上链接
 *
 * @param unknown $tags
 * @return string
 */
function tag_to_url($tags) {
	$str = '';
	$tag = explode ( ',', str_replace ( ' ', ',', $tags ) );
	foreach ( $tag as $k => $v ) {
		if ($_REQUEST ['mobile']) {
			$str .= '<a href="' . C ( 'site_url' ) . '/tag/' . $v . '/?mobile=1">' . $v . '</a> ';
		} else {
			$str .= '<a href="' . C ( 'site_url' ) . '/tag/' . $v . '/">' . $v . '</a> ';
		}
	}

	return $str;
}

/**
 * 获取token
 *
 * @param unknown $str
 * @param number $time
 * @return string
 */
function get_token($str, $time = 0) {
	$str = md5 ( $str . $time );
	if ($time) {
		$str .= '_' . $time;
	}
	return $str;
}

/**
 * 匹配token
 *
 * @param unknown $str
 * @param unknown $token
 * @return string
 */
function check_token($str, $token) {
	$arr = explode ( '_', $token );
	$time = intval ( $arr [1] );
	$code = md5 ( $str . $time );
	$token = $arr [0];
	if ($code != $token) {
		$result ['status'] = 0;
		$result ['msg'] = 'token不匹配';
	} else if (time () > $time) {
		$result ['status'] = 0;
		$result ['msg'] = 'token过了有效期';
	} else {
		$result ['status'] = 1;
		$result ['msg'] = 'token匹配通过';
	}
	return $result;
}

/**
 * 检查是否是以手机浏览器进入
 *
 * @return boolean
 */
function is_mobile() {
	$mobile = array ();
	static $mobilebrowser_list = 'Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
	// note 获取手机浏览器
	if (preg_match ( "/$mobilebrowser_list/i", $_SERVER ['HTTP_USER_AGENT'], $mobile )) {
		return true;
	} else {
		if (preg_match ( '/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER ['HTTP_USER_AGENT'] )) {
			return false;
		} else {
			return false;
		}
	}
}
/**
 * URL重定向
 *
 * @param unknown $url
 */
function check_mobile() {
	$url = C ( 'site_url' ) . $_SERVER ['REQUEST_URI'];
	if (strpos ( $url, '?' )) {
		$url .= '&mobile=1';
	} else {
		$url .= '?mobile=1';
	}
	if (is_mobile () && ! $_GET ['mobile'] && $url) {
		header ( 'location:' . $url );
		exit ();
	}
	return $url;
}

/**
 * 获取网页url
 */
function get_server_url() {
	$host = $_SERVER ['HTTP_HOST'];
	$url = $_SERVER ['REDIRECT_SCRIPT_URI'] ? $_SERVER ['REDIRECT_SCRIPT_URI'] : (strlen ( $_SERVER ['REDIRECT_URL'] ) > 10 ? "http://{$host}" . $_SERVER ['REDIRECT_URL'] : "http://{$host}" . $_SERVER ['REQUEST_URI']);
	return $url;
}

/**
 * 生成缓存名字
 *
 * @param unknown $name
 * @return string
 */
function get_cache_name($name) {
	$name = md5 ( $name );
	return $name;
}

/**
 * 获取用户ip
 *
 * @return Ambigous <string, unknown>
 */
function get_user_ip() {
	if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
		$ip = getenv ( 'HTTP_CLIENT_IP' );
	} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
		$ip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
		$ip = getenv ( 'REMOTE_ADDR' );
	} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
		$ip = $_SERVER ['REMOTE_ADDR'];
	}
	return preg_match ( "/[\d\.]{7,15}/", $ip, $matches ) ? $matches [0] : '0.0.0.0';
}
